<?php

	header('Content-Type: text/html; charset=utf-8');

	require_once(dirname(__FILE__)."/../core/safeParam.php");
	require_once(dirname(__FILE__).'/../core/database.php');
	require_once(dirname(__FILE__).'/../core/functions.php');


	if (!ini_get('register_globals')) {
		$superglobals = array($_SERVER, $_ENV, $_FILES,$_SESSION,$_POST,$_GET);
  		
  		if (isset($_SESSION)) {
  			array_unshift($superglobals, $_SESSION);
  		}
  		
  		foreach ($superglobals as $superglobal) {
  			extract($superglobal, EXTR_SKIP);
  		} 
  		
  		ini_set('register_globals', true);
	} 
	
	if (isset($_SESSION['password'])) {
		//define a restaurant
		if (isset($_GET['site']) && $_GET['site'] != '') {
			$_SESSION['arid'] = $_GET['site'];
		}
		
		if (! isset($_SESSION['arid']) or $_SESSION['arid'] == '') {
			if (isset($_SESSION['admin'])){
				$_SESSION['arid'] = $_SESSION['admin'];
			} else {
				$_SESSION['arid'] = 1;
			}
		} else { 
			define("ARID", $_SESSION['arid']);
  			
  			$config = $database->select("SELECT * FROM config where id = " . ARID . "");
  			$query = $database->query("SELECT * FROM config WHERE id =" . ARID . "");
  
  			while ($list = mysql_fetch_array($query)) {
    			$home = $list['home'];
  			} 

  			$staffUsers = getUserAuthorizationsInRestaurant();
  			$username = $_SESSION['username'];
  			
  			if (array_key_exists( $username, $staffUsers)) {
    			$user = $staffUsers[$username];
			} 
						  			
  			$displayClass = "";
  
  			function getActivityAuthorizations($user){
  				$activities = array();
  				$activities['menu'] = "";
   			$activities['homePage'] = "";
   			$activities['siteDocuments'] = "";
   			$activities['restaurant'] = "";
   			$activities['gateways'] = "";
   			$activities['users'] = "";
   			$activities['coupons'] = "";
   			$activities['orders'] = "";
   			$activities['orderstatus'] = "";
   			$activities['locations'] = "";
   			$activities['reports'] = "";
   			$activities['ads'] = "";
   			$activities['news'] = "";
   			$activities['import'] = "";
   			$activities['tools'] = "";
   			$activities['plugins'] = "";
		   
		   	if (isset($user)) {
		   		foreach ($activities as $key=>$value){
		   			if (array_key_exists($key, $user)){
		   				if ( $user['authorized'][$key] == "checked" ){
		   					$activities[$key] = "";
		   				} else {
		   					$activities[$key] = "hidden";
		   				}
						} 
    				}
    			}
    		
    			return $activities;
    		}
			
			$activities = array();
			if(isset($user)){
				$activities = getActivityAuthorizations($user);
			}

		}
	} 
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
	<title>Delivery351 - Administracion del Local -</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

	<!--// SCRIPTS FOR DROPDOWN AND TABBED INTERFACE -->
		<style type="text/css"></style>
	<!--// FOLLOWING SCRIPT IS FOR PNG FIX IE5.5/IE6-->

	<!--[if lt IE 7]>
		<script defer type="text/javascript" src="js/pngfix.js"></script> 
	<![endif]--> 

	<link href="../lib/bootstrap/css/bootstrap.css" rel="stylesheet" type="text/css" />
	<link href="../lib/jqueryui/css/flick/jquery-ui-1.8.21.custom.css" rel="stylesheet" type="text/css" />

	<link href="css/styles.css" rel="stylesheet" type="text/css" />
	<link rel="stylesheet" href="css/jquery.ui.timepicker.css?v=0.3.1" type="text/css" />

	<link rel="icon" type="image/png" href="../favicon.png">
		
	<script type="text/javascript" src="../lib/jquery/jquery.1.7.2.min.js"></script>
	<script type="text/javascript" src="../lib/jqueryui/js/jquery-ui-1.8.21.custom.min.js"></script>
	<script type="text/javascript" src="../lib/jquerytimepicker/jquery.ui.timepicker.js"></script>
	
	<script type="text/javascript" src="js/dropmenu.js"></script>
	<script type="text/javascript" src="js/site.js"></script>

	<script type="text/javascript" src="../jquery/js/jquery-1.9.0.js"></script>
	<script type="text/javascript" src="../jquery/js/include/jquery-1.5.1.min.js"></script>

	<script type="text/javascript" src="../jquery/js/jquery-ui-1.10.0.custom.js"></script>
	<script type="text/javascript" src="../jquery/js/jquery-ui1.10.0.custom.min.js"></script>
	<script type="text/javascript" src="../jquery/js/jquery-ui.js"></script>
	<script type="text/javascript" src="../jquery/js/include/jquery.ui.core.min.js"></script>
	<script type="text/javascript" src="../jquery/js/include/jquery.ui.position.min.js"></script>
	<script type="text/javascript" src="../jquery/js/include/jquery.ui.tabs.min.js"></script>
	<script type="text/javascript" src="../jquery/js/include/jquery.ui.widget.min.js"></script>


   <script type="text/javascript" src="js/jquery.ui.timepicker.js?v=0.3.1"></script>
    
   <script type="text/javascript">
   	$(document).ready(function() {
    		$('#floating_timepicker').timepicker({
    			onSelect: function(time, inst) {
    				$('#floating_selected_time').html('You selected ' + time);
    			}
    		});
      	$('#tabs').tabs();
		});
	</script>
	    
</head>

<body>
	<div id="contenedor">
		<?php
			if (isset($_SESSION['password'])) {
		?>
			<div class="adminTop">
				
				<!--// LOGO-->
				<div class="adminLogo">
					<img src="../UserFiles/Image/product_photos/<?php echo $config[0]['logo']; ?>"  />
				</div>
				<!--// FIN LOGO		-->
				
				
				<!--// SELECTOR DE SITIOS		-->
				<div class="selectorSitios">
			
					<!--//	BIENVENIDO	-->
   				<div class="textoSelectorSitios">
						<b>Bienvenido</b> <?php echo $_SESSION['username']?> 
					</div>
					<!--//	FIN BIENVENIDO	-->
				
					<?php
						if (isset($_SESSION['password']) && $_SESSION['admin'] == 1 && (! isset($_SESSION['staffUser'])) ) {
					?>
							<div class="menuSelectorSitios">
								<form name="jump1">
									<select name="myjumpbox" id="myjumpbox" OnChange="location.href=jump1.myjumpbox.options[selectedIndex].value" >
					<?php
										$sites = $database->select("SELECT * from config order by sitename");
										for ($s=0; $s< count($sites); $s++) {
											if (ARID == $sites[$s]['id']) {
												$selected[$s] = "selected=selcted";
											}
									
											echo '
											<option value="index.php?site='. $sites[$s]['id'] .'"  name="jumpurl2" ' . $selected[$s] . '>
												'. $sites[$s]['sitename'] .'
											</option>';
										}	 
					?>
									</select>
								</form>	
							</div>
					<?php
						} // if isset();
					?>
				</div>
				<!--// FIN SELECTOR DE SITIOS		-->	
				
				
				
				<!--// CONTENEDOR ICONOS TOP		-->
				<div class="contenedorIconosTop">
					<ul>
					
						<li>
							<!--// VER PAGINA-->
							<div class="cajaIconosTop">
								<div class="iconoCajaIconosTop">
									<a href="../sites.php?site=<?php echo $_SESSION['arid'] ?>" target="_blank">
										<img src="images/icons/big_visitsite.png" alt="big_visitsite"  />
									</a>
								</div>
	
								<div class="textoCajaIconosTop">
									<a href="../sites.php?site=<?php echo $_SESSION['arid'] ?>" target="_blank">
										Ver página
									</a>
								</div>			
   						</div>
						</li>					
					
					
					
						<li>
							<!--// CONFIGURACION-->
							<div class="cajaIconosTop">
      						<div class="iconoCajaIconosTop">
									<a href="index.php?edit=config">
										<img src="images/icons/big_settings.png" alt="big_settings"  />
									</a>
								</div>
		      				
      						<div class="textoCajaIconosTop">
									<a href="index.php?edit=config">
										Configuración
									</a>
      						</div>
      					</div>
						</li>
						
						

						<li>
							<!--// CERRAR SESION-->
							<div class="cajaIconosTop">
								<div class="iconoCajaIconosTop">
									<a href="../functions/logout.php">
   									<img src="images/icons/big_logout.png" alt="big_logout"  />
   								</a>
								</div>

								<div class="textoCajaIconosTop">
									<a href="../functions/logout.php">
										Cerrar Sesión
									</a>
								</div>
							</div>
						</li>
					</ul>
				</div>
				<!--// FIN CONTENEDOR ICONOS TOP		-->
		</div>
		<!--// FIN TOP -->
		
		
		<!--// AVISO DE ACCIONES DE CAMBIOS-->
		<?php
			$message = "";
			$errorClass = "";

			if (isset($_GET['updated']) && $_GET['updated'] == 1) {
				$message = "Los cambios han sido guardados";
			} else {
				if (isset($_SESSION['error'])){
					$message = $_SESSION['error'];
    				unset($_SESSION['error']);
    				$errorClass = "errorMessage";
    			}
			}
				
			if ($message){
		?>
		<!--// DIV DE AVISO DE ACCIONES -->
				<div class="accionesDeCambio">
					<img src="images/icons/positive.png" />
					<?php echo $message; ?>
				</div>
		<?php
			} //if(message) 
		?>
			<!--// FIN AVISO DE ACCIONES DE CAMBIOS-->



		<!--// 	CONTENIDO CENTRAL 	-->
		<div class="contenidoCentral">
			<!--// 	COLUMNA IZQUIERDA 	-->		
			<div class="columnaIzquierda">
		
				<div id="leftcolumn">
  					<?php  										
						if (isset($_SESSION['password'])) {
								
							function renderMenuItem($cssClass, $authorizationKey, $menuAction, $text){
								return '<a class="'.$cssClass.' '.$authorizationKey.'" href="index.php?edit='.$menuAction.'">'.$text.'</a>';
      	  				}
      	  								
      	  				$cant_pedidos = 0;
      	  								
      	  				if($_SESSION['arid'] != 1 ) {
      	  					$results = $database->select("SELECT * FROM order_sum WHERE rid = ".ARID." ORDER by date desc ");
												
								if($results != false){
									for($i=0; $i< count($results); $i++){
										if($results[$i]['status'] == 0) {
											$cant_pedidos++;
										}
									}  
								}
      	  				}
																								      	  		
      	  				$menu = '<div class="menuIzquierda">';
      	  				$menu .= renderMenuItem("plugins", "inicio", "inicio", "Estado del sitio");
      	  				$menu .= renderMenuItem("config", "restaurant", "config", "Configuración Principal");
							$menu .= renderMenuItem("locations", "locations", "locations", "Ubicación");
          				$menu .= renderMenuItem("home_page", "homePage", "home", "Página Principal");				

							$menu .= renderMenuItem("menu", "menu", "menu", "Menú");				
							
							if($cant_pedidos == 0){
								$menu .= renderMenuItem("orders", "orders", "orders", "Pedidos");
							} else{
								$menu .= renderMenuItem("orders", "orders", "orders", 
								"Pedidos &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
								<span style='background-color:#C11; border-radius:4px; color:#FFF; font-weight:bold;
									font-family:sans-serif; font-size:10px; padding:2px; '>
								&nbsp;&nbsp;". $cant_pedidos ."&nbsp;&nbsp; 
								</span>");
							}								 

							$menu .= renderMenuItem("news", "news", "news", "Noticias");


							$menu .= renderMenuItem("reports", "reports", "reports", "Reportes");

							if (isset($_SESSION['password']) && $_SESSION['admin'] == 1 && (! isset($_SESSION['staffUser'])) ) {
								//$menu .= renderMenuItem("global_menu_options", "menu", "global-options", "Opciones Globales");
		         			//$menu .= renderMenuItem("site_documents", "siteDocuments", "pages", "Páginas del Sitio");
		         			$menu .= renderMenuItem("order_status", "orderstatus", "order_status", "Estados de Pedidos");
								$menu .= renderMenuItem("users", "users", "users", "Usuarios");
          					//$menu .= renderMenuItem("ad_manager", "ads", "ads", "Agregar Banner");
          					//$menu .= renderMenuItem("import", "import", "import", "Importar");
         					//$menu .= renderMenuItem("tools", "tools", "tools", "Tools");
							}		         		

          				$menu .= "</div>";
							echo $menu;
        				}
					?>
				</div>
				<!--// FIN LEFTCOLUMN-->
			</div>
			<!--// 	FIN COLUMNA IZQUIERDA 	-->
			
			
			
			<!--// 	COLUMNA CENTRAL 	-->
			<div class="columnaCentral">
		

			<!--// CONTENIDO	PRINCIPAL		-->								
			<?php 
				if(isset($_GET['edit'])){
					$edit = $_GET['edit'];
				}
				
				switch($edit) {
					
					default:
						include "inicio.php";
					break;
					
					case "pages":
      				include 'pages.php';
      			break;
    
    				case "users":
      				include 'users.php';
      			break;
    
    				case "gateway":
     					include 'gateway.php';
     				break;
    
					case "menu":
     					include 'menu.php';
     				break;
    
   				case "orders":
     					include 'ordersreg.php';
      			break;
    
    				case "locations":
      				include 'locations.php';
      			break;
        
    				case "config":
      				include 'config.php';
      			break;
    
    				case "reports":
      				include 'reports.php';
      			break;
    
    				case "ads":
      				include 'ads.php';
      			break;
    
    				case "global-options":
      				include 'global-options.php';
      			break;
    
    				case "import":
      				include 'import.php';
      			break;
    
       			case "news":
      				include 'news.php';
      			break;
    
    				case "order_status":
      				include 'order_status.php';
      			break;
      				
      			case "home":
      				include 'home.php';
      			break;
    		
		   		case "tools":
      				include 'tools.php';
      			break;
    
    				case "plugins":
      				include 'plugins.php';
		      	break;
				}
			?>
			<!--// FIN CONTENIDO PRINCIPAL	-->
			
		</div>		
		<!--// 	FIN COLUMNA CENTRAL 	-->
		
		
			</div>
			<!--// 	FIN CONTENIDO PRINCIPAL 	-->
		
		
		

		<!--// 	LOG-IN -	-->
		<?php
			} else {
			//if isset(password) 
		?>
			<div class="loginAdmin">				
				<div class="loginAdminTitulo">
  					<h2>
  						Iniciar Sesión
  					</h2>
  				</div>
      						
      		<div class="loginAdminForm">
      			<form method="post" action="../functions/login.php">
						<div class="loginAdminCampos">
							Usuario:
							<br />
    						<input type="text" id="username" name="username" />
      				</div>	
      								
      				<div class="loginAdminCampos">
							Contraseña:
							<br />
    						<input id="password" name="password" type="password" />
      				</div>
      								
      				<div class="loginAdminCampos">
	      				<a href="../functions/pages.php?function=reset-password">¿Olvidaste tu contraseña?</a>
      				</div>
      				
      				<?php
							if (!isset($_SESSION['password'])) {
								if (isset($error)) {
									echo '
									<div class="loginAdminError">
										Error: usuario y/o contraseña
									</div>';
  								}
  							} 
							?>   
      								
      				<div class="loginAdminCampos">
	      				<input type="submit" value="Entrar"/>
      				</div>
					</form>
      		</div>
      		<!--// FIN FORMULARIO LOG-IN 	-->
      	</div>
		<?php
			}
		?>
		<!--// FIN LOG-IN 	-->	
	</div>
	<!--// FIN CONTENEDOR 	-->
</body>

</html>